package com.swpu.controller;

import com.swpu.dao.XyhUserDao;
import com.swpu.entity.XyhUserEntity;
import com.swpu.util.XyhUserResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/xyh/myapp")
public class XyhUserController {

    /**
     * 帮助我们自动new SwpuUserDao
     * 暂时只有dao层和server层通过这种方式进行new
     */
    @Resource
    private XyhUserDao xyhUserDao;

    @GetMapping(value = "/findAll")
    public XyhUserResult finAll() {
        XyhUserResult result = new XyhUserResult();
        List<XyhUserEntity> list = null;
        try {
            list = xyhUserDao.findAll();
        } catch (Exception e) {
            result.setStatus(false);
            result.setData(-1);
            result.setMessage(e.getMessage());
        }
        if (list.isEmpty()){
            result.setStatus(false);
            result.setData(0);
            result.setMessage("数据库中还没有用户！");
        }else{
            result.setStatus(true);
            result.setData(list);
            result.setMessage("查询成功！");
        }
        return result;
    }

    /**
     * @param id
     * @return
     */
    @GetMapping(value = "/findById")
    public Object findById(@RequestParam(value = "id") String id) {
        XyhUserResult result = new XyhUserResult();
        Object i = null;
        boolean flag =false;
        try {
            i = xyhUserDao.findById(id);
            if (i == null){
                flag = false;
            }else{
                flag = true;
            }
        } catch (Exception e) {
            result.setStatus(false);
            result.setData(-1);
            result.setMessage(e.getMessage());
        }
        if (!flag){
            result.setStatus(false);
            result.setData( 0);
            result.setMessage("不存在该用户！");
        }else{
            result.setStatus(true);
            result.setData(i);
            result.setMessage("查询成功！");
        }
        return result;
    }

    /**
     * @param xyhuserentity {@link com.swpu.entity.XyhUserEntity}
     * @return {@link com.swpu.util.XyhUserResult}
     */
    @PostMapping(value = "/insert")
    public XyhUserResult insert(@RequestBody XyhUserEntity xyhuserentity) {
        XyhUserResult result = new XyhUserResult();
        int i = 0;
        Object j = 0;
        try {
            j = xyhUserDao.findById(xyhuserentity.getId());
        } catch (Exception e) {
            result.setStatus(false);
            result.setData(-1);
            result.setMessage(e.getMessage());
        }
        if (j == "" | j == null) {
            try {
                i = xyhUserDao.insertUser(xyhuserentity);
            } catch (Exception e) {
                result.setStatus(false);
                result.setData(-1);
                result.setMessage(e.getMessage());
            }
            if (i > 0) {
                result.setStatus(true);
                result.setData(1);
                result.setMessage("插入成功！");
            }
        }else{
            result.setStatus(false);
            result.setData(-2);
            result.setMessage("插入失败，已存在重复用户！");
        }
        return result;
    }

    @PutMapping(value = "/update")
    public XyhUserResult updateUser(@RequestBody XyhUserEntity xyhUserEntity) {
        XyhUserResult result = new XyhUserResult();
        int i = 0;
        Object j = 0;
        j = findById(xyhUserEntity.getId());
        if (j == "" | j == null) {
            result.setStatus(false);
            result.setData(0);
            result.setMessage("没有找到该用户！");
        } else {
            try {
                i = xyhUserDao.updateUser(xyhUserEntity);
            } catch (Exception e) {
                result.setStatus(false);
                result.setData(-1);
                result.setMessage(e.getMessage());
            }
            if (i > 0) {
                result.setStatus(true);
                result.setData(1);
                result.setMessage("更新成功！");
            }
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    public XyhUserResult deleteById(@RequestParam(value = "id") String id) {
        XyhUserResult result = new XyhUserResult();
        int i = 0;
        Object j = findById(id);
        if (j == "" | j == null) {
            result.setStatus(false);
            result.setData(0);
            result.setMessage("不存在该用户！");
        } else {
            try {
                i = xyhUserDao.deleteUserById(id);
            } catch (Exception e) {
                result.setStatus(false);
                result.setData(-1);
                result.setMessage(e.getMessage());
            }
            if (i > 0) {
                result.setStatus(true);
                result.setData(1);
                result.setMessage("删除成功！");
            }
        }
        return result;
    }
}
